package cn.yfj.org.service.impl;

import cn.yfj.basic.config.BusinessException;
import cn.yfj.basic.service.impl.BaseServiceImpl;
import cn.yfj.basic.util.BaiduAuditUtils;
import cn.yfj.org.domain.Employee;
import cn.yfj.org.domain.Shop;
import cn.yfj.org.domain.ShopAuditLog;
import cn.yfj.org.vo.ShopVo;
import cn.yfj.org.mapper.EmployeeMapper;
import cn.yfj.org.mapper.ShopAuditLogMapper;
import cn.yfj.org.mapper.ShopMapper;
import cn.yfj.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yfj
 * @since 2022-02-11
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired //发送邮件
    JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        //判断不能够为空值
        if (StringUtils.isEmpty(shop.getName())) {
            throw new BusinessException("不能够为空值");

        }
        //判断文字是否包含非法字符
        if (BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BusinessException("违禁词*****");
        }
        //判断店铺是否被注册
        Shop shopTmp = shopMapper.loadByName(shop.getName());
        if (shopTmp != null) {
            throw new BusinessException("店铺已存在请登录");
        }
        //employee是否可以注册
        Employee admin = shop.getAdmin();
        Employee empTmp = employeeMapper.loadByUsename(admin.getUsername());
        if (empTmp != null && empTmp.getShop_id() != null) {
            throw new BusinessException("管理员已不能管理多个店铺");
        }
        if (empTmp == null) {
            //新增员工
            //2.存储employee
            employeeMapper.save(admin);//返回自增ID
            empTmp = admin;
        }
        shop.setAdminId(empTmp.getId());
        //设置状态为待审核
        shop.setState(0);
        String logo = shop.getLogo();
        shop.setLogo("http://47.95.117.210:8888"+logo);

        shopMapper.save(shop);
        //需要重新修改employee中的shopId
        empTmp.setShop_id(shop.getId());
        employeeMapper.update(empTmp);
    }

    @Override
    @Transactional
    public void pass(ShopAuditLog shopAuditLog) {
        //修改状态为审核通过
        Long shopId = shopAuditLog.getShopId();
        //查询出需要修改的记录的数据
        Shop shop = shopMapper.loadById(shopId);
        //修改状态为审核通过
        shop.setState(1);
        //修改
        shopMapper.update(shop);
        //设置审核的人
        shopAuditLog.setAuditId(333L);
        //保存
        shopAuditLogMapper.save(shopAuditLog);
        //查询对应数据
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("3239448854@qq.com");
        message.setTo("3239448854@qq.com");
        message.setSubject("店铺入驻审核");
        message.setText(shopAuditLog.getNote());
        javaMailSender.send(message);

    }

    @Override
    @Transactional
    public void reject(ShopAuditLog shopAuditLog) {
        //修改状态为审核通过
        Long shopId = shopAuditLog.getShopId();
        //查询出需要修改的记录的数据
        Shop shop = shopMapper.loadById(shopId);
        //修改状态为审核驳回
        shop.setState(-1);
        //修改
        shopMapper.update(shop);
        //设置审核的人
        shopAuditLog.setAuditId(333L);
        //保存
        shopAuditLogMapper.save(shopAuditLog);
        /*发送邮件*/
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            helper.setFrom("3239448854@qq.com");
            helper.setSubject("邮件标题");
            //第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>店铺入驻审核详情</h1>" +
                    "<a href='http://127.0.0.1:8081/#/register2?id=" + shop.getId() + "'>点击修改资料</a>", true);
            //收件人
            helper.setTo("3239448854@qq.com");
            //发送邮件
            javaMailSender.send(mimeMessage);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<ShopVo> queryStateCount() {
        
        return shopMapper.queryStateCount();
    }
}
